import { postRequest, getRequest, windowOpen } from "../http.js";
import axios from "axios";
import qs from "qs";
// import { getParamsDatas } from "@/utils/sid"
//获取路由参数
function getParamsDatas(str) {
    var arr = str.split("?");

    if (arr.length == 2) {
        var data = arr[1].split("&");
        var obj = {};
        data.forEach(function (item) {
            var params = item.split("=");

            obj[params[0]] = params[1];
        });
        return {
            url: arr[0], //url
            data: obj, //参数
        };
    } else {
        return {
            url: arr[0], //无参数,
            data: {},
        };
    }
}
//获取路由参数
const sid = getParamsDatas(location.href).data.soleId;

let request = (config) => {
    // 1.创建axios的实例
    const instance = axios.create({
        // 设置基础的url配置项，这样接口处的url前面就不用写url:'http://127.0.0.1:8000/api/home'，直接写成 url:'/api/home', 就可以了
        // 本机
        // baseURL: 'http://192.168.3.58:8098/',//思宇
        // baseURL: 'http://192.168.10.208:8098/',
        baseURL: "https://ibms.on-bright.com:8000/",
        // baseURL: "http://192.168.203.56/80",
        //设置请求超时时间
        timeout: 5000,
    });
    // 2.axios的拦截器，用不到的可以忽略这节
    // 2.1.请求拦截的作用
    instance.interceptors.request.use(
        (config) => {
            config.headers.__sid = sid;
            // config.headers.__sid = '269c774d-40f0-4c76-9c48-4c3074c4398a'
            console.log(config);
            return config;
        },
        (err) => {
            console.log("请求拦截err: " + err);
        }
    );
    // 2.2.响应拦截
    instance.interceptors.response.use(
        (res) => {
            console.log("res", res);
            const { data, config } = res;
            console.log("config", config);
            if (config.headers.responseType === "blob") {
                return res;
            }
            return res;
        },
        (err) => {
            console.log("响应拦截err22: " + err);
        }
    );
    // 3.发送真正的网络请求
    return instance(config);
};
// 统计告警
let getlist = (data) => {
    return new Promise((reslove, reject) => {
        postRequest("./msgCenterService/msg/list", data).then((res) => {
            // postRequest("./msg/list", data).then(res => {
            // console.log(getParamsDatas(location.href).data.soleId)
            console.log(location.href);
            reslove(res);
        });
    });
};
let commonlist = (data) => {
    return new Promise((reslove, reject) => {
        postRequest("./msgCenterService/common/list", data).then((res) => {
            // postRequest("./common/list", data).then(res => {
            reslove(res);
        });
    });
};
//增改
let getgridsave = (data) => {
    return new Promise((reslove, reject) => {
        postRequest("./msgCenterService/msg/gridsave", data).then((res) => {
            reslove(res);
        });
    });
};
//删除
let getdeletes = (data) => {
    return new Promise((reslove, reject) => {
        postRequest("./msgCenterService/msg/deletes", data).then((res) => {
            reslove(res);
        });
    });
};
//查询消息总数
let getrecord = (data) => {
    return new Promise((reslove, reject) => {
        postRequest("./msgCenterService/msg/record", data).then((res) => {
            reslove(res);
        });
    });
};
//查询告警等级统计
let getregister = (data) => {
    return new Promise((reslove, reject) => {
        postRequest("./msgCenterService/msg/register", data).then((res) => {
            reslove(res);
        });
    });
};
//查询告警等级统计
let getalarmType = (data) => {
    return new Promise((reslove, reject) => {
        postRequest("./msgCenterService/msg/alarmType", data).then((res) => {
            reslove(res);
        });
    });
};
//查询告警分类
let getAlarmCategory = (data) => {
    return new Promise((reslove, reject) => {
        postRequest("./msgCenterService/msg/getAlarmCategory", data).then(
            (res) => {
                reslove(res);
            }
        );
    });
};
//查询告警级别
let getAlarmLevel = (data) => {
    return new Promise((reslove, reject) => {
        postRequest("./msgCenterService/msg/getAlarmLevel", data).then(
            (res) => {
                reslove(res);
            }
        );
    });
};
let getbroken = (data) => {
    return new Promise((reslove, reject) => {
        postRequest("./msgCenterService/msg/broken", data).then((res) => {
            reslove(res);
        });
    });
};

//转工单信息
let updateAlarmMsgStatus = (data) => {
    return new Promise((reslove, reject) => {
        postRequest(
            "/propertyService/alarmMsg/updateAlarmMsgStatus",
            data
        ).then((res) => {
            reslove(res);
        });
    });
};

//导出
let getexpro = (data) => {
    return request({
        method: "post",
        // url: "/msg/expro",
        url: "/msgCenterService/msg/expro",
        responseType: "blob",
        // responseType : 'arraybuffer',
        headers: {
            "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        },
        // withCredentials:true,
        data: qs.stringify(data),
        // data:data,
    });
};

//获取树结构
let getTree = (tableName, funId, pId) => {
    return new Promise((reslove, reject) => {
        postRequest(
            `/msgCenterService/msg/tree/load?tableName=${tableName}&funId=${funId}&pId=${pId}`
        ).then((res) => {
            reslove(res);
        });
    });
};

export {
    getlist,
    getgridsave,
    getdeletes,
    getrecord,
    getregister,
    getalarmType,
    getbroken,
    getexpro,
    commonlist,
    getAlarmCategory,
    getAlarmLevel,
    updateAlarmMsgStatus,
    getTree,
};
